#include "Client.h"
#include "UDPSocket.h"
#include "Parser.h"
#include "ServerParam.h"
#include "PlayerParam.h"
#include "CommandSender.h"
#include "States/FullState.h"


#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>

#include <cmath>
#include <ctime>
#include <cstdlib>

#include <QThread>

using namespace std;

Client::Client()
{
    srand( time(0) );
    srand48( time(NULL) );

    //------------------INIT--------------

}

Client::~Client()
{

}

int Client::Connect()
{
    UDPSocket::instance().Initial(ServerParam::instance().getHost().c_str(), ServerParam::instance().getPort());

    stringstream init_string;
    init_string << "(init ";
    init_string << PlayerParam::instance().getTeamName();
    init_string << "(version 14)";
    init_string << ")";
    UDPSocket::instance().Send(init_string.str().c_str());

    return 0;
}


//Formation :)
int Client::Move()
{
    printf("Moving the player\n");
    stringstream init_string;
    init_string << "(move ";
    init_string << rand()%70 - 20 << " " << rand()%60-30 << ")";
    UDPSocket::instance().Send(init_string.str().c_str());

    return 0;
}

void Client::getInit()
{
    char msg[10000];
    UDPSocket::instance().Receive(msg);

    int i = 0;
    char *pch = strtok(msg," ");
    while (pch != NULL)
    {
        if (i == 0)
        {
            //printf("pch = %s\n", pch);
            if (strcmp(pch, "(init") == 0)
            {
                //printf("Initialized!");
                PlayerParam::instance().setConnected(true);
            }
        }

        if (i == 1)
        {
            if (!strcmp(pch, "l"))
            {
                PlayerParam::instance().setSide(0);
            }
            else
            {
                PlayerParam::instance().setSide(1);
            }
        }

        if (i == 2)
        {
            printf("Player: %s\n", pch);
            PlayerParam::instance().setNum(atoi(pch));
        }

        ++i;
        pch = strtok(NULL," ");
    }
}

int Client::init()
{
    int cTry = 0;
    do
    {
        if (++cTry > 20)
        {
            printf ("Connect to server error\n");
            return 0;
        }

        Connect();
        getInit();
    } while(!PlayerParam::instance().isConnected());

    printf("Connected to Server\n");

    mParser = new Parser();
    mCommandSender = new CommandSender();
    mParser->start();

    return 1;
}


/*
void Client::getPlayMode(string &s)
{
    FullState::instance().mutex.lock();
    s = FullState::instance().getPlayMode();
    FullState::instance().mutex.unlock();
}
*/

//void Client::run()
//{
//    printf("Client running\n");

//    int cTry = 0;
//    do
//    {
//        if (++cTry > 20)
//        {
//            printf ("Connect to server error\n");
//            return ;
//        }

//        Connect();
//        getInit();
//    } while(!PlayerParam::instance().isConnected());

//    printf("Connected to Server\n");

//    mParser = new Parser();

//    mCommandSender = new CommandSender();

//    mParser->start();



//    int step = 0;
//    while(1)
//    {
//        //play_mode?

//        if (step % 500 == 0)
//        {
//            //FullState::instance().mutex.lock();
//            //printf("PLAY_MODE %s\n", FullState::instance().getPlayMode().c_str());
//            //FullState::instance().mutex.unlock();

//            /*
//            FullState::instance().mutex.lock();
//            cout << "Ball Position: " << FullState::instance().getBall().getPosition() << endl;
//            cout << "Agent Position: " << FullState::instance().getPlayer(0, PlayerParam::instance().getNum()).getPosition() << endl;
//            FullState::instance().mutex.unlock();
//            */
//        }
//        //sleep(ServerParam::SIMULATOR_STEP/100.0/2.0);
//        Action a(Dash, 100, 0);
//        mCommandSender->sendActionT(a);
//        mCommandSender->sendActionT(a);
//        mCommandSender->sendActionT(a);

//        Action b(Turn, 0, rand() % 360 - 180);
//        mCommandSender->sendActionT(b);
//        ++step;
//    }


//}
